package com.zhoutian.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.zhoutian.constant.MessageConstant;
import com.zhoutian.entity.PageResult;
import com.zhoutian.entity.QueryPageBean;
import com.zhoutian.entity.Result;
import com.zhoutian.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户操作
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Reference//查找服务
    private UserService userService;

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    //获得当前登录用户的用户名
    @RequestMapping("/getUsername")
    public Result getUsername(){
        //当Spring security完成认证后，会将当前用户信息保存到框架提供的上下文对象
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        System.out.println(user);
        if(user != null){
            String username = user.getUsername();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,username);
        }

        return new Result(false, MessageConstant.GET_USERNAME_FAIL);
    }

    //新增用户
    @PreAuthorize("hasAuthority('USER_ADD')")//权限校验
    @RequestMapping("/add")
    public Result add(@RequestBody com.zhoutian.pojo.User user, Integer[] rId){
        try{
            Integer id = rId[0];
            //加密密码
            user.setPassword(passwordEncoder.encode(user.getPassword()));
            return userService.add(user,id);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_USER_FAIL);//新增用户失败
        }
    }

    //删除用户
    @PreAuthorize("hasAuthority('USER_DELETE')")//权限校验
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try{
            userService.deleteById(id);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.DELETE_USER_FAIL);//删除用户失败
        }
        return  new Result(true, MessageConstant.DELETE_USER_SUCCESS);//删除用户成功
    }

    //分页查询
    @PreAuthorize("hasAuthority('USER_QUERY')")//权限校验
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return userService.pageQuery(queryPageBean);
    }

    //编辑用户
    @PreAuthorize("hasAuthority('USER_EDIT')")//权限校验
    @RequestMapping("/edit")
    public Result edit(@RequestBody com.zhoutian.pojo.User user, Integer[] rId){
        try{
            Integer id = rId[0];
            //加密密码
            user.setPassword(passwordEncoder.encode(user.getPassword()));
            return userService.edit(user, id);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_USER_FAIL);//编辑用户失败
        }
    }

    //根据ID查询用户
    @PreAuthorize("hasAuthority('USER_QUERY')")//权限校验
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try{
            com.zhoutian.pojo.User user = userService.findById(id);
            return new Result(true,MessageConstant.QUERY_USER_SUCCESS,user);//查询用户成功
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_USER_FAIL);//查询用户失败
        }
    }

    //根据用户ID查询关联的角色ID
    @PreAuthorize("hasAuthority('USER_QUERY')")//权限校验
    @RequestMapping("/findRoleIdByUserId")
    public Result findRoleIdByUserId(Integer id) {
        try{
            List<Integer> rId = userService.findRoleIdByUserId(id);
            return new Result(true,MessageConstant.QUERY_USER_SUCCESS,rId);//查询用户成功
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_USER_FAIL);//查询用户失败
        }
    }

    //根据用户名查询是否存在
    @PreAuthorize("hasAuthority('USER_QUERY')")//权限校验
    @RequestMapping("/findUserName")
    public Result findUserName(String username){
        return userService.findUserName(username);
    }

}
